package com.upcode.weighing.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.upcode.weighing.domain.SysRes;
import com.upcode.weighing.domain.SysRoleRes;
import com.upcode.weighing.mapper.SysRoleResMapper;
import com.upcode.weighing.service.SysRoleResService;
import com.upcode.weighing.vo.add.SysRoleResAddVo;
import com.upcode.weighing.vo.del.BaseDelVo;
import com.upcode.weighing.vo.resp.pc.ResStatusList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色资源表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2019-04-10
 */
@Service
@Transactional
public class SysRoleResServiceImpl extends ServiceImpl<SysRoleResMapper, SysRoleRes> implements SysRoleResService {

    @Autowired
    private SysRoleResMapper sysRoleResMapper;

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public int addRoleRes(SysRoleResAddVo vo) {
        Long roleId= vo.getRoleId();//获取角色ID
        ResStatusList[] resIds=vo.getResIds();//获取添加的资源ID
        List<SysRoleRes> list=new ArrayList<SysRoleRes>();
        for(int i=0;i<resIds.length;i++){
            SysRoleRes sysRoleRes=new SysRoleRes();
            sysRoleRes.setId(IdWorker.getId());//添加角色-资源ID
            sysRoleRes.setRoleId(roleId);//添加角色ID
            sysRoleRes.setResId(resIds[i].getId());//添加资源ID
            sysRoleRes.setSel(resIds[i].getSel());//选中状态
            list.add(sysRoleRes);
        }
        return sysRoleResMapper.addRoleRes(list);
    }
    //根据角色名称获取所有资源
    @Override
    public List<SysRes> getResByRole(SysRoleResAddVo vo) {
        List<SysRes> list=sysRoleResMapper.getResByRole(vo.getRoleId());
        List<SysRes> list1=new ArrayList<SysRes>();
        for(SysRes r:list){
            if(r.getPid()==0){
                list1.add(r);
            }
        }
        for(SysRes r:list1){
            r.setChildRes(getChild(r.getId(),list));
        }
        return list1;
    }

    //根据角色ID获取资源ID列表
    @Override
    public List<ResStatusList> getResIdByRoleId(Long rId) {
        return sysRoleResMapper.getResIdByRoleId(rId);
    }
    //删除菜单时解除菜单与角色的绑定
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public int delResRole(BaseDelVo vo) {
        return sysRoleResMapper.delResRole(vo.getIds());
    }

    //解除角色下绑定的资源
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public int delResRoleByRid(Long id) {
        return sysRoleResMapper.delResRoleByRid(id);
    }

    //递归查找子菜单
    private List<SysRes> getChild(Long id,List<SysRes> list){
        List<SysRes> childList = new ArrayList<SysRes>();
        for(SysRes r:list){
            if(r.getPid().equals(id)){
                childList.add(r);
            }
        }
        for(SysRes r:childList){
            r.setChildRes(getChild(r.getId(),list));
        }
        if(childList.size()==0){
            return null;
        }
        return childList;
    }
}
